using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace RTFConvertor
{    
    public class Convertor
    {
        private string rtfText = string.Empty;
        private string htmlText = string.Empty;
        private static Microsoft.Office.Interop.Word.Application WordObj = null;
        
        private Microsoft.Office.Interop.Word.Document WordDoc = null;
        private RichTextBox richtxtBox = new RichTextBox();
        private Encoding currentEncoding = Encoding.GetEncoding(1252);

        public string ConvertRTF2HTML(string rtfText)
        {
            string tempFilePath = System.IO.Path.GetTempFileName();
            string tempFilePathHtml = System.IO.Path.GetTempFileName();

            richtxtBox.Rtf = rtfText;
            richtxtBox.SaveFile(tempFilePath + ".rtf");

            object nullobj = System.Reflection.Missing.Value;
            object rtfPath = tempFilePath + ".rtf";

            if (WordObj == null)
            WordObj = new Microsoft.Office.Interop.Word.Application();

        try
        {
            WordDoc = WordObj.Documents.Open(ref rtfPath,
                        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);

        }
        catch (System.Runtime.InteropServices.COMException)
        {
            
        
            WordObj = new Microsoft.Office.Interop.Word.Application();
            WordDoc = WordObj.Documents.Open(ref rtfPath,
                       ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                       ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                       ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
        
        }
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML;

            object filePath = tempFilePathHtml + ".html";

            WordDoc.SaveAs(ref filePath, ref format, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);

            object routeDoc = true;
            object obj1 = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
            object obj2 = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;


            WordDoc.Close(ref obj1, ref obj2, ref routeDoc);


            StreamReader reader = new StreamReader(tempFilePathHtml + ".html", currentEncoding);
            File.OpenText(tempFilePathHtml + ".html");
            string htmlText = File.ReadAllText(tempFilePathHtml + ".html", currentEncoding);
            return htmlText; 
        
        }

        public string ConvertHTML2RTF(string htmlText)
        {
            string tempFilePath = System.IO.Path.GetTempFileName();
            string tempFilePathRtf = System.IO.Path.GetTempFileName();

            StreamWriter stringWriter = new StreamWriter(tempFilePath + ".html", false, currentEncoding);
            stringWriter.Write(htmlText);
            stringWriter.Close();

            
           

            object nullobj = System.Reflection.Missing.Value;
            object htmlPath = tempFilePath + ".html";
            if (WordObj == null)
                WordObj = new Microsoft.Office.Interop.Word.Application();

            try
            {
                WordDoc = WordObj.Documents.Open(ref htmlPath,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                
                WordObj = new Microsoft.Office.Interop.Word.Application();
                WordDoc = WordObj.Documents.Open(ref htmlPath,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            }

            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;
           
            object filePath = tempFilePathRtf + ".rtf";

            WordDoc.SaveAs(ref filePath, ref format, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
                        , ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            

            object routeDoc = true;
            object obj1 = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
            object obj2 = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;

            WordDoc.Close(ref obj1, ref obj2, ref routeDoc);

            StreamReader reader = new StreamReader(tempFilePathRtf + ".rtf", currentEncoding);
            richtxtBox.Rtf = string.Empty;
            richtxtBox.LoadFile(tempFilePathRtf + ".rtf");

            return richtxtBox.Rtf;

        }

        public string ConvertHTML2Text(string htmlText)
        {
            string tempFilePath = System.IO.Path.GetTempFileName();
            string tempFilePathRtf = System.IO.Path.GetTempFileName();

            StreamWriter stringWriter = new StreamWriter(tempFilePath + ".html", false, currentEncoding);
            stringWriter.Write(htmlText);
            stringWriter.Close();




            object nullobj = System.Reflection.Missing.Value;
            object htmlPath = tempFilePath + ".html";
            if (WordObj == null)
                WordObj = new Microsoft.Office.Interop.Word.Application();

            try
            {
                WordDoc = WordObj.Documents.Open(ref htmlPath,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            }
            catch (System.Runtime.InteropServices.COMException)
            {

                WordObj = new Microsoft.Office.Interop.Word.Application();
                WordDoc = WordObj.Documents.Open(ref htmlPath,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            }
            try
            {
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;

            object filePath = tempFilePathRtf + ".txt";

            WordDoc.SaveAs(ref filePath, ref format, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
                        , ref nullobj, ref nullobj, ref nullobj, ref nullobj);

            
                object routeDoc = true;
                object obj1 = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
                object obj2 = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;

                WordDoc.Close(ref obj1, ref obj2, ref routeDoc);
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;

                object filePath = tempFilePathRtf + ".txt";

                WordDoc.SaveAs(ref filePath, ref format, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
                            , ref nullobj, ref nullobj, ref nullobj, ref nullobj);


                object routeDoc = true;
                object obj1 = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
                object obj2 = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;

                WordDoc.Close(ref obj1, ref obj2, ref routeDoc);
            }
            StreamReader reader = new StreamReader(tempFilePathRtf + ".txt", currentEncoding);

            return reader.ReadToEnd();

        }
        public static bool isWordOpen()
        {
            if (WordObj == null)
                return false;
            else
                return true;
        }
        public static void CloseWordInstance()
        {
            
                object routeDoc = true;
                object obj1 = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
                object obj2 = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
                try
                {
                    WordObj.Quit(ref obj1, ref obj2, ref routeDoc);
                }
                catch (System.Runtime.InteropServices.COMException)
                {
                    
                }
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(WordObj);
                WordObj = null;
                System.GC.Collect();
                        
        }
    }
}
